package ophiux.common.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class MapUtil {

	public static boolean isEmpty(Map map){
		boolean flag = false;
		if(map == null){
			flag = true;
			return flag;
		}
		return flag;
	}
	
	/**
	 * 对象转为Map(值不为空)
	 * @param pojo
	 * @return
	 */
	public static HashMap<String, String> toMap(Object pojo) {
		HashMap<String, String> hashMap = new HashMap<String, String>();
		try {
			if (pojo == null)
				return hashMap;
			Method method[] = pojo.getClass().getDeclaredMethods();
			Field field[] = pojo.getClass().getDeclaredFields();
			
			List<Field> fieldList = new ArrayList<Field>();
			
			Class clz = pojo.getClass();
			while(clz != Object.class) {
				fieldList.addAll(Arrays.asList(clz.getDeclaredFields()));
				clz = clz.getSuperclass();
			}
			
			for (Field fd : fieldList) {
				String prpName = fd.getName();
				String getMetd = "get"
						+ fd.getName().substring(0, 1).toUpperCase()
						+ fd.getName().substring(1);
				Method tmethod = pojo.getClass().getMethod(getMetd);
				Object v =  tmethod.invoke(pojo);
				
			//	fd.setAccessible(true);
			//	Object v = fd.get(pojo);
				if (v != null) {
					hashMap.put(prpName, v+"");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}

		return hashMap;
	}
	
	/**
	 * 对象转为TreeMap(值不为空)
	 * 首字母排序
	 * @param pojo
	 * @return
	 */
	public static SortedMap<String, String> toSortedMap(Object pojo) {
		SortedMap<String, String> treeMap = new TreeMap<String,String>();
		if (pojo == null)
			return treeMap;
		Method method[] = pojo.getClass().getDeclaredMethods();
		Field field[] = pojo.getClass().getDeclaredFields();
		for (Method md : method) {
			for (Field fd : field) {
				String prpName = fd.getName();
				String getMetd = "get"
						+ fd.getName().substring(0, 1).toUpperCase()
						+ fd.getName().substring(1);
				if (getMetd.equals(md.getName())) {
					try {
						Object v = md.invoke(pojo, new Object[0]);
						if (v != null) {
							treeMap.put(prpName, v+"");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return treeMap;
		
		
	}
	
	public static Map<String, String> map2Bean(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, String> map = new HashMap<String, String>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object object = getter.invoke(obj);
                    if (object != null) {
                        String value = object.toString();
                        map.put(key, value);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }
        return map;
    }
	
	/**
	 * Description:将json字符串转为SortedMap <br>
	 * 创 建 人: hhl
	 * 创建日期:2018年7月19日 上午9:38:37
	 * @param param
	 * @return
	 */
	public static SortedMap<String, String> JSONOToSortedMap(String param) {
		SortedMap<String, String> treeMap = new TreeMap<String,String>();
	//	treeMap = JSONObject.parseObject(param, new TypeReference<TreeMap<String, String>>(){});
		return treeMap;
	}
	
}
